package com.junyuan.gsp.web;

import com.junyuan.gsp.bean.Product;
import com.junyuan.gsp.bean.Result;
import com.junyuan.gsp.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: junyuan
 */
@RestController
public class ProductController
{
	@Autowired
	private ProductService productService;

	@GetMapping("/products/{id}")
	public Result findProductById(@PathVariable Integer id)
	{
		Product product = productService.findProductById(id);
		return new Result(product);
	}

	@GetMapping("/products")
	public Result findProducts(Integer page, Integer limit, String commonName, String productName, String spec, Integer state)
	{
		Map<String, Object> map = new HashMap<>();
		map.put("page", page);
		map.put("limit", limit);
        map.put("commonName", commonName);
        map.put("productName", productName);
        map.put("spec", spec);
        map.put("state", state);
		return productService.findProductsResult(map);
	}

	@DeleteMapping("/products/{id}")
	public Result deleteProductById(@PathVariable Integer id)
	{
		productService.deleteProductById(id);
		return Result.SUCCESS;
	}

	@PutMapping("/products")
	public Result saveProduct(Product product)
	{
		productService.saveProduct(product);
		return Result.SUCCESS;
	}

}
